
* Formatting
graph set window fontface "Times New Roman"	// use Times New Roman as the default font for Graph window
// 	 graph set window fontface default 			// use this to restore to factury setting if preferred later

set scheme s1mono

use "${ddata}cleaned_data_wACSweight.dta", replace

gen donation_1=qh1
gen donation_2=qh2 if male==0
replace donation_2=qh3 if male==1
gen sample_all=1
keep userid sce treatment donation_1 donation_2 sample_all weight_ACS
sort userid sce
keep if donation_1!=.|donation_2~=.
save temp_d, replace


use "${ddata}cleaned_data_wACSweight.dta", replace
gen donation_1=qh1
gen donation_2=qh2 if male==0
replace donation_2=qh3 if male==1

keep if donation_1!=.|donation_2~=.
append using temp_d

gen sample=1 if sample_all==1
replace sample=2 if male==0
replace sample=3 if male==1

forvalues i=1(1)3 {

	forvalues j=1(1)2 {

		reg donation_`j' treatment if sample==`i' [aw=weight_ACS], robust

		matrix q`i'`j' = r(table)

		gen  p_`i'`j'=  q`i'`j'[4,1]
	}
}  
keep userid   donation_1 donation_2 p_* treatment sce sample weight_ACS



reshape long donation_ p_1 p_2 p_3,  i( userid sce treatment sample) j(question)
rename donation_ donation
gen p_value=p_1 if sample==1
replace p_value=p_2 if sample==2
replace p_value=p_3 if sample==3


collapse (mean) mean= donation p_value (sd) sd=donation   (count) n=donation [aw=weight_ACS], by(question treatment sample)


generate hiwrite = mean + invttail(n-1,0.025)*(sd / sqrt(n))
generate lowrite = mean - invttail(n-1,0.025)*(sd / sqrt(n))

generate q_t = 1    if question==1&treatment==0&sample==1
replace q_t = 2    if question==1&treatment==1&sample==1

replace q_t = 4    if question==1&treatment==0&sample==2
replace q_t = 5    if question==1&treatment==1&sample==2


replace q_t = 7    if question==1&treatment==0&sample==3
replace q_t = 8    if question==1&treatment==1&sample==3


replace q_t = 11    if question==2&treatment==0&sample==1
replace q_t = 12    if question==2&treatment==1&sample==1

replace q_t = 14    if question==2&treatment==0&sample==2
replace q_t = 15    if question==2&treatment==1&sample==2


replace q_t = 17    if question==2&treatment==0&sample==3
replace q_t = 18    if question==2&treatment==1&sample==3


sort q_t

gen position=1.5 if question==1&sample==1
replace position=4.5 if question==1&sample==2
replace position=7.5 if question==1&sample==3

replace position=11.5 if question==2&sample==1
replace position=14.5 if question==2&sample==2
replace position=17.5 if question==2&sample==3

gen _plotp =""

forvalues i=1(1)2 {
	forvalues j=1(1)3 {
		egen max_`i'`j'=max(hiwrite) if question==`i'&sample==`j'
		gen ppv_`i'`j'=p_value if question==`i'&sample==`j'
		egen pv_`i'`j'=max(ppv_`i'`j')
		local p`i'`j': display %4.3f pv_`i'`j' //store p-value with correct formating in local macro
		replace _plotp = "p=`p`i'`j''" if question==`i'&sample==`j' //store p-value with brackets as string variable
	}
}

gen y=.
forvalues i=1(1)2 {
	forvalues j=1(1)3 {
		replace y=max_`i'`j'+4 if question==`i'&sample==`j'
	}
}

gen yy=hiwrite+0.2
gen label=strofreal(mean,"%03.1f")


twoway (bar mean q_t if treatment==0, color(black%30) ) ///
	(bar mean q_t if treatment==1, color(black%60) ) ///
	(scatter  y position, msym(none) mlab(_plotp) mlabp(12) mlabc(black) mlabs(vsmall)) ///
	(scatter  yy q_t, msym(none) mlab(label) mlabp(12) mlabc(black) mlabs(vsmall)) ///
	(rcap hiwrite lowrite q_t, lcolor(gray)), ///
	legend( order(1 "Control" 2 "Treatment" ) ) ///
	xlabel( 1.5 "All" 4.5 "Women" 7.5 "Men" 11.5 "All" 14.5 "Women" 17.5 "Men", noticks  labsize(small)) ///
	ylabel(0(10)100,labsize(small)) yscale(r(0 100)) ///
	xtitle("{bf:First Order Beliefs                                                        Second Order Beliefs}", size(small)) legend(region(lstyle(none)) size(small)) ytitle("Share of US$100 donated", size(small)) ///

graph export "${o}donations_weighted.pdf", replace
